local skel = fk.CreateSkill {
  name = "lb__zaohuang",
  tags = { Skill.Limited }
}

Fk:loadTranslationTable {
  ["lb__zaohuang"] = "凿荒",
  --[":lb__zaohuang"] = "<b>限定技</b>，出牌阶段，你可以减1点体力上限，然后直到你的下回合开始前，你可以如手牌般使用装备区中的非装备牌，且在每次<a href=':lb__tianji'><font color='red'>〖天击〗</font></a>结算后重置<a href=':lb__tianji'><font color='red'>〖天击〗</font></a>次数。你杀死角色后复原此技能。",
  --9.22
  [":lb__zaohuang"] = "<b>限定技</b>，出牌阶段，你可以减1点体力上限，然后直到你的下回合开始前，你可以如手牌般使用装备区中的非装备牌，且在每次<a href=':lb__tianji'><font color='red'>〖天击〗</font></a>结算后重置<a href=':lb__tianji'><font color='red'>〖天击〗</font></a>次数。你杀死角色后回满体力并重置此技能的发动次数。",


  ["lb__zaohuang_view"] = "凿荒",
  [":lb__zaohuang_view"] = "你可以使用装备区中的非装备牌。",

  ["#lb__zaohuang_filter"] = "凿荒",
  ["#lb__zaohuang_view"] = "凿荒：你可以如手牌般使用装备区中的非装备牌",
  ["#lb__zaohuang"] = "凿荒：你可以减1点体力上限，然后直到你的下回合开始前，你可以如手牌般使用装备区中的非装备牌，且在每次〖天击〗结算后重置〖天击〗次数",
  ["@@lb__zaohuang"] = "凿荒",

  ["$lb__zaohuang1"] = "大捷，已定！",
  ["$lb__zaohuang2"] = "翾翔不坠，万载常胜！",
}


skel:addEffect("active", {
  prompt = "#lb__zaohuang",
  can_use = function(self, player)
    return player:usedSkillTimes("lb__zaohuang", Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    room:setPlayerMark(player, "@@lb__zaohuang", 1)
    room:changeMaxHp(player, -1)
    room:handleAddLoseSkills(player, "lb__zaohuang_view", "lb__zaohuang", false, true)
  end,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@lb__zaohuang") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@lb__zaohuang", 0)
    player.room:handleAddLoseSkills(player, "-lb__zaohuang_view", "lb__zaohuang", false, true)
  end,
})

skel:addEffect(fk.EventLoseSkill, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@lb__zaohuang") > 0 and data.skill.name == "lb__zaohuang_view"
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "lb__zaohuang_view", "lb__zaohuang", false, true)
  end,
})

skel:addEffect(fk.Death, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill("lb__zaohuang") and data.damage and data.damage.from and data.damage.from == player
  end,
  on_use = function(self, event, target, player, data)
    player:setSkillUseHistory("lb__zaohuang", 0, Player.HistoryGame)
    if not player.dead and player:isWounded() then
      player.room:recover {
        who = player,
        num = player:getLostHp(),
        skillName = skel.name,
        recoverBy = player,
      }
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(skel.name, true) then return end
    for _, move in ipairs(data) do
      return (move.from == player and table.find(move.moveInfo, function(info)
        return info.fromArea == Card.PlayerEquip
      end)) or (move.to == player and move.toArea == Card.PlayerEquip)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getBanner("lb__zaohuang_" .. player.id) or {} --p(room:getBanner(skel.name..player.id) or {})
    for _, id in ipairs(player:getCardIds("e")) do
      local card = Fk:getCardById(id, true)
      if card.type ~= Card.TypeEquip and not table.find(cards, function(cid)
            local clone = Fk:getCardById(cid, true)
            return clone:getMark("lb_zaohuang") == id
          end) then -- 不重复印卡
        local clone = room:printCard(card.name, card.suit, card.number)
        room:setCardMark(clone, "lb_zaohuang", id)
        table.insert(cards, clone.id)
      end
    end
    room:setBanner("lb__zaohuang_" .. player.id, cards)
  end,
})


return skel
